package com.appclub.kitsilano.calendar;

import java.util.Calendar;
import com.appclub.kitsilano.R;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public final class CalendarFragment extends Fragment {
	private static final String KEY_CONTENT = "TestFragment:Content";
	private static int d;
	private int Days_Size;
	private int screenWidth;
	private String mContent;
	private int lastY;
	private int RLH;
	private String[] Days;
	private GridView gridView;
	private View view;
	private int dy2;
	private Boolean B_st = false;
	private Boolean B_D = true;
	private Boolean B_T = true;
	private int Duration = 900;
	private int dy;
	private int bottom1;

	public static CalendarFragment newInstance(String content) {
		CalendarFragment fragment = new CalendarFragment();
		StringBuilder builder = new StringBuilder();
		builder.append(content).append("  ");
		fragment.mContent = builder.toString();
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if ((savedInstanceState != null)
				&& savedInstanceState.containsKey(KEY_CONTENT)) {
			mContent = savedInstanceState.getString(KEY_CONTENT);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		final Calendar c = Calendar.getInstance();
		if (mContent.indexOf("Jan") >= 0) {
			c.set(Calendar.MONTH, 0);
		}
		if (mContent.indexOf("Feb") >= 0) {
			c.set(Calendar.MONTH, 1);
		}
		if (mContent.indexOf("Mar") >= 0) {
			c.set(Calendar.MONTH, 2);
		}
		if (mContent.indexOf("Apr") >= 0) {
			c.set(Calendar.MONTH, 3);
		}
		if (mContent.indexOf("May") >= 0) {
			c.set(Calendar.MONTH, 4);
		}
		if (mContent.indexOf("Jun") >= 0) {
			c.set(Calendar.MONTH, 5);
		}
		if (mContent.indexOf("Jul") >= 0) {
			c.set(Calendar.MONTH, 6);
		}
		if (mContent.indexOf("Aug") >= 0) {
			c.set(Calendar.MONTH, 7);
		}
		if (mContent.indexOf("Sep") >= 0) {
			c.set(Calendar.MONTH, 8);
		}
		if (mContent.indexOf("Oct") >= 0) {
			c.set(Calendar.MONTH, 9);
		}
		if (mContent.indexOf("Nov") >= 0) {
			c.set(Calendar.MONTH, 10);
		}
		if (mContent.indexOf("Dec") >= 0) {
			c.set(Calendar.MONTH, 11);
		}
		c.set(Calendar.DATE, 1);
		d = c.get(Calendar.DAY_OF_WEEK);
		Days = new String[] { "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
				"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
				"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
				"0", "0", "0", "0", "0", "0", "0", "0" };
		if (d == Calendar.SUNDAY) {
			int days_m = c.getActualMaximum(Calendar.DATE);
			for (int i = 1; i <= days_m; i++) {
				Days[i - 1] = Integer.toString(i);
			}
		}
		if (d == Calendar.MONDAY) {
			int days_m = c.getActualMaximum(Calendar.DATE);
			for (int i = 1; i <= days_m; i++) {
				Days[i] = Integer.toString(i);
			}
		}
		if (d == Calendar.TUESDAY) {
			int days_m = c.getActualMaximum(Calendar.DATE);
			for (int i = 1; i <= days_m; i++) {
				Days[i + 1] = Integer.toString(i);
			}
		}
		if (d == Calendar.WEDNESDAY) {
			int days_m = c.getActualMaximum(Calendar.DATE);
			for (int i = 1; i <= days_m; i++) {
				Days[i + 2] = Integer.toString(i);
			}
		}
		if (d == Calendar.THURSDAY) {
			int days_m = c.getActualMaximum(Calendar.DATE);
			for (int i = 1; i <= days_m; i++) {
				Days[i + 3] = Integer.toString(i);
			}
		}
		if (d == Calendar.FRIDAY) {
			int days_m = c.getActualMaximum(Calendar.DATE);
			for (int i = 1; i <= days_m; i++) {
				Days[i + 4] = Integer.toString(i);
			}
		}
		if (d == Calendar.SATURDAY) {
			int days_m = c.getActualMaximum(Calendar.DATE);
			for (int i = 1; i <= days_m; i++) {
				Days[i + 5] = Integer.toString(i);
			}
		}
		view = inflater.inflate(R.layout.calendar, container, false);
		gridView = (GridView) view.findViewById(R.id.gridView1);
		WindowManager wm = (WindowManager) view.getContext().getSystemService(
				Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		screenWidth = display.getWidth();

		if (screenWidth == 240) {
			Days_Size = ((screenWidth - dptopx(view, 6)) / 7);
			gridView.setHorizontalSpacing(dptopx(view, 1 / 2));
			gridView.setVerticalSpacing(dptopx(view, 1));

			gridView.setColumnWidth(screenWidth - dptopx(view, 2));
		} else {
			Days_Size = ((screenWidth - dptopx(view, 12)) / 7);
			gridView.setHorizontalSpacing(dptopx(view, 2));
			gridView.setVerticalSpacing(dptopx(view, 2));
			gridView.setColumnWidth(screenWidth - dptopx(view, 4));
		}

		gridView.setAdapter(new CalendarAdapter(view.getContext(), Days, 90));
		gridView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				TextView textView = (TextView) v.findViewById(R.id.text_item);
				if (textView.getText() == "") {
				} else {
					gridView.invalidateViews();
					gridView.setAdapter(new CalendarAdapter(view.getContext(),
							Days, position));
				}
			}
		});

		for (int i = 1; i <= 7; i++) {
			String dd = "days_textView" + i;
			int resID = getResources().getIdentifier(dd, "id",
					"com.appclub.kitsilano");
			TextView t = (TextView) view.findViewById(resID);
			t.setWidth(Days_Size);
		}

		final RelativeLayout rl = (RelativeLayout) view
				.findViewById(R.id.RLayout);
		final RelativeLayout rrl = (RelativeLayout) view
				.findViewById(R.id.layout);
		final LinearLayout l = (LinearLayout) view.findViewById(R.id.L);
		final Button b = (Button) view.findViewById(R.id.handlebutton);
		final ScrollView SV = (ScrollView) view.findViewById(R.id.scrollView1);
		// ---------------------------ScrollView-----------------------------------------------------
		SV.setOnTouchListener(new View.OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				SV.layout(SV.getLeft(), SV.getTop(), SV.getRight(),
						rl.getBottom());
				l.layout(l.getLeft(), l.getTop(), l.getRight(), rl.getBottom()
						- b.getHeight());

				int ea = event.getAction();
				switch (ea) {
				case MotionEvent.ACTION_DOWN:
					lastY = (int) event.getRawY();
					RLH = rl.getHeight();
					bottom1 = b.getBottom();
					if (b.getTop() > RLH / 2
							&& b.getTop() <= gridView.getBottom()
									+ rrl.getHeight()) {
						B_D = true;
					}
					if (b.getTop() == RLH / 2) {
						B_st = true;
					}
					break;
				case MotionEvent.ACTION_MOVE:
					int Y = (int) event.getRawY() - lastY;
					dy = Y;

					int top = b.getTop() + dy;
					int bottom = b.getBottom() + dy;
					int top2 = SV.getTop() + dy;
					if (top < RLH / 2) {
						top = RLH / 2;
						bottom = top + b.getHeight();
						top2 = bottom;
					}
					if (bottom > bottom1) {
						top = b.getTop();
						bottom = top + b.getHeight();
						top2 = bottom;
					}
					if (b.getTop() <= RLH / 2) {
						if (dy > 0) {
							top = b.getTop();
							bottom = b.getBottom();
							top2 = bottom;
						}
					}
					b.layout(b.getLeft(), top, b.getRight(), bottom);
					SV.layout(SV.getLeft(), top2, SV.getRight(), rl.getBottom());
					l.layout(l.getLeft(), l.getTop(), l.getRight(),
							rl.getBottom());
					lastY = (int) event.getRawY();
					break;

				case MotionEvent.ACTION_UP:
					int btop = b.getTop();
					final int HRLH = RLH / 2;
					if (b.getTop() > rrl.getHeight() + gridView.getHeight()) {
						b.layout(
								b.getLeft(),
								gridView.getBottom() + rrl.getHeight(),
								b.getRight(),
								gridView.getBottom() + rrl.getHeight()
										+ b.getHeight());
						SV.layout(SV.getLeft(), b.getBottom(), SV.getRight(),
								rl.getHeight());
						l.layout(l.getLeft(), l.getTop(), l.getRight(),
								rl.getHeight());

						Animation a = new TranslateAnimation(0, 0, btop
								- b.getTop(), gridView.getBottom()
								+ rrl.getHeight() - b.getTop());
						a.setDuration(Duration);
						a.setInterpolator(AnimationUtils.loadInterpolator(
								b.getContext(),
								android.R.anim.bounce_interpolator));
						b.startAnimation(a);
						SV.startAnimation(a);
					}
					if (b.getTop() > RLH / 2
							&& b.getTop() <= gridView.getBottom()
									+ rrl.getHeight() && B_D) {
						b.layout(v.getLeft(), HRLH, b.getRight(),
								HRLH + b.getHeight());
						SV.layout(SV.getLeft(), b.getBottom(), SV.getRight(),
								rl.getHeight());
						l.layout(l.getLeft(), l.getTop(), l.getRight(),
								rl.getHeight());

						Animation a = new TranslateAnimation(0, 0, btop
								- b.getTop(), RLH / 2 - b.getTop());
						a.setDuration(Duration);
						a.setInterpolator(AnimationUtils.loadInterpolator(
								v.getContext(),
								android.R.anim.bounce_interpolator));
						b.startAnimation(a);
						SV.startAnimation(a);
						B_T = true;
					}
					break;
				}
				return false;
			}
		});
		// ---------------------------handle-----------------------------------------------------
		b.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(final View v, MotionEvent event) {
				int ea = event.getAction();
				switch (ea) {
				case MotionEvent.ACTION_DOWN:
					v.setBackgroundDrawable(v.getResources().getDrawable(
							R.drawable.handle_c));
					lastY = (int) event.getRawY();
					RLH = rl.getHeight();
					if (b.getTop() > RLH / 2
							&& b.getTop() <= gridView.getBottom()
									+ rrl.getHeight()) {
						B_D = true;
					}
					if (b.getTop() == RLH / 2) {
						B_st = true;
					}
					break;
				case MotionEvent.ACTION_MOVE:
					if (b.getTop() > RLH / 2
							&& b.getTop() <= gridView.getBottom()
									+ rrl.getHeight()) {
						B_D = true;
					}
					v.setBackgroundDrawable(v.getResources().getDrawable(
							R.drawable.handle_c));
					dy = (int) event.getRawY() - lastY;
					int top = v.getTop() + dy;
					int bottom = v.getBottom() + dy;
					int top2 = SV.getTop() + dy;
					if (top < 0) {
						top = 0;
						bottom = top + v.getHeight();
						top2 = bottom;
					}
					if (top > rrl.getHeight() + gridView.getHeight()) {
						dy2 = dy / 2;
						top = v.getTop() + dy2;
						bottom = v.getBottom() + dy2;
						top2 = SV.getTop() + dy2;
					}
					v.layout(v.getLeft(), top, v.getRight(), bottom);
					SV.layout(SV.getLeft(), top2, SV.getRight(), rl.getBottom());
					l.layout(l.getLeft(), l.getTop(), l.getRight(),
							rl.getBottom());
					lastY = (int) event.getRawY();
					break;
				case MotionEvent.ACTION_UP:
					int btop = b.getTop();
					final int HRLH = RLH / 2;
					v.setBackgroundDrawable(v.getResources().getDrawable(
							R.drawable.handle_n));
					if (v.getTop() > rrl.getHeight() + gridView.getHeight()) {
						B_D = false;
						v.layout(
								v.getLeft(),
								gridView.getBottom() + rrl.getHeight(),
								v.getRight(),
								gridView.getBottom() + rrl.getHeight()
										+ v.getHeight());
						SV.layout(SV.getLeft(), v.getBottom(), SV.getRight(),
								rl.getHeight());
						l.layout(l.getLeft(), l.getTop(), l.getRight(),
								rl.getHeight());

						Animation a = new TranslateAnimation(0, 0, btop
								- b.getTop(), gridView.getBottom()
								+ rrl.getHeight() - b.getTop());
						a.setDuration(Duration);
						a.setInterpolator(AnimationUtils.loadInterpolator(
								v.getContext(),
								android.R.anim.bounce_interpolator));
						v.startAnimation(a);
						SV.startAnimation(a);
					}
					if (btop < HRLH) {
						if (B_T) {
							B_T = false;
							v.layout(v.getLeft(), rrl.getTop(), v.getRight(),
									rrl.getTop() + b.getHeight());
							SV.layout(SV.getLeft(), v.getBottom(),
									SV.getRight(), rl.getHeight());
							l.layout(l.getLeft(), l.getTop(), l.getRight(),
									rl.getHeight());

							Animation a = new TranslateAnimation(0, 0, btop
									- b.getTop(), 0 - b.getTop());
							a.setDuration(Duration);
							a.setInterpolator(AnimationUtils.loadInterpolator(
									v.getContext(),
									android.R.anim.bounce_interpolator));
							v.startAnimation(a);
							SV.startAnimation(a);
							B_D = false;
						} else {
							B_D = false;
							B_T = true;
							v.layout(
									v.getLeft(),
									rrl.getHeight() + gridView.getHeight(),
									v.getRight(),
									rrl.getHeight() + gridView.getHeight()
											+ b.getHeight());
							SV.layout(SV.getLeft(), v.getBottom(),
									SV.getRight(), rl.getHeight() * 2);
							l.layout(l.getLeft(), l.getTop(), l.getRight(),
									rl.getHeight() * 2);

							Animation a = new TranslateAnimation(0, 0, btop
									- b.getTop(), rrl.getHeight()
									+ gridView.getHeight() - b.getTop());
							a.setDuration(Duration);
							a.setInterpolator(AnimationUtils.loadInterpolator(
									v.getContext(),
									android.R.anim.bounce_interpolator));
							v.startAnimation(a);
							SV.startAnimation(a);
						}
					}
					if (b.getTop() > RLH / 2
							&& b.getTop() <= gridView.getBottom()
									+ rrl.getHeight() && B_D) {
						v.layout(v.getLeft(), HRLH, v.getRight(),
								HRLH + b.getHeight());
						SV.layout(SV.getLeft(), v.getBottom(), SV.getRight(),
								rl.getHeight());
						l.layout(l.getLeft(), l.getTop(), l.getRight(),
								rl.getHeight());

						Animation a = new TranslateAnimation(0, 0, btop
								- b.getTop(), RLH / 2 - b.getTop());
						a.setDuration(Duration);
						a.setInterpolator(AnimationUtils.loadInterpolator(
								v.getContext(),
								android.R.anim.bounce_interpolator));
						v.startAnimation(a);
						SV.startAnimation(a);
						B_T = true;
					}
					if (b.getTop() == RLH / 2 && B_st && B_D) {
						v.layout(
								v.getLeft(),
								gridView.getBottom() + rrl.getHeight(),
								v.getRight(),
								gridView.getBottom() + b.getHeight()
										+ rrl.getHeight());
						SV.layout(SV.getLeft(), v.getBottom(), SV.getRight(),
								rl.getHeight() * 2);
						l.layout(l.getLeft(), l.getTop(), l.getRight(),
								rl.getHeight() * 2);

						Animation a = new TranslateAnimation(0, 0, btop
								- b.getTop() + rrl.getHeight(), gridView
								.getBottom() + rrl.getHeight() - b.getTop());
						a.setDuration(Duration);
						a.setInterpolator(AnimationUtils.loadInterpolator(
								v.getContext(),
								android.R.anim.bounce_interpolator));
						v.startAnimation(a);
						SV.startAnimation(a);
						B_st = false;
					}
					break;
				}
				return false;
			}
		});
		return view;

	}

	private int dptopx(View view, int i) {
		DisplayMetrics dm = new DisplayMetrics();
		dm = view.getResources().getDisplayMetrics();

		int dp2px = (int) (i * dm.density + 0.5f);

		return dp2px;
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString(KEY_CONTENT, mContent);
	}
}